package com.example.zhidao.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.zhidao.dao.BlogMapper;
import com.example.zhidao.dao.ClassifyMapper;
import com.example.zhidao.model.po.Admin;
import com.example.zhidao.model.po.Blog;
import com.example.zhidao.model.responseData.ResponseData;
import com.example.zhidao.service.AdminService;
import com.example.zhidao.service.BlogService;
import com.example.zhidao.service.UserService;
import com.example.zhidao.utils.Ujwt;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@CrossOrigin
@RequestMapping("/echart")
@RestController
public class EchaetController {//5

    @Autowired
    private AdminService adminService;
    @Autowired
    private UserService userService;
    @Autowired
    private BlogService blogService;
    @Autowired
    private Ujwt ujwt;

    @Resource
    private BlogMapper blogMapper;


//    获取上面四个数据
    @GetMapping("/admin/getfour")
    public ResponseData getfour(@RequestHeader String token){

        Claims claims=(Claims)ujwt.tokenToOut(token);//强制转换属性
        String aid=claims.getId();
        QueryWrapper<Admin> q=new QueryWrapper();
        q.eq("id",aid);
        if(adminService.getOne(q)==null){
            return new ResponseData(null,false,"你不是管理员",300);
        }

        Map<String, Object> keyValueMap = new HashMap<>();

        keyValueMap.put("userNum",userService.list().size());
        keyValueMap.put("adminNum",adminService.list().size());
        keyValueMap.put("blogNum",blogService.list().size());

        QueryWrapper<Blog> qw=new QueryWrapper<>();
        qw.eq("type",2);
        keyValueMap.put("awaitBlogNum",blogService.list(qw).size());

        return new ResponseData(keyValueMap,true,"true",200);
    }


//    获取近一个月创建的博客
    @GetMapping("/admin/getMonthBlog")
    public ResponseData getMonthBlog(@RequestHeader String token){
        Claims claims=(Claims)ujwt.tokenToOut(token);//强制转换属性
        String aid=claims.getId();
        QueryWrapper<Admin> q=new QueryWrapper();
        q.eq("id",aid);
        if(adminService.getOne(q)==null){
            return new ResponseData(null,false,"你不是管理员",300);
        }

        return new ResponseData(blogMapper.getMonthBlog(),true,"获取成功",200);
    }

//    获取前前十个博客贡献者

    @GetMapping("/admin/getBlogUserNumVo")
    public ResponseData getBlogUserNumVo(@RequestHeader String token){
        Claims claims=(Claims)ujwt.tokenToOut(token);//强制转换属性
        String aid=claims.getId();
        QueryWrapper<Admin> q=new QueryWrapper();
        q.eq("id",aid);
        if(adminService.getOne(q)==null){
            return new ResponseData(null,false,"你不是管理员",300);
        }

        return new ResponseData(blogMapper.getBlogUserNumVo(),true,"获取成功",200);
    }

//    获取各个分类的博客数据
    @GetMapping("/admin/classifyNum")
    public ResponseData getclassifyNum(@RequestHeader String token){
        Claims claims=(Claims)ujwt.tokenToOut(token);//强制转换属性
        String aid=claims.getId();
        QueryWrapper<Admin> q=new QueryWrapper();
        q.eq("id",aid);
        if(adminService.getOne(q)==null){
            return new ResponseData(null,false,"你不是管理员",300);
        }
        return new ResponseData(blogMapper.getClassifyVo(),true,"获取成功",200);
    }

//    获取各个状态的博客数
@GetMapping("/admin/typeNum")
public ResponseData getTypeNum(@RequestHeader String token){
    Claims claims=(Claims)ujwt.tokenToOut(token);//强制转换属性
    String aid=claims.getId();
    QueryWrapper<Admin> q=new QueryWrapper();
    q.eq("id",aid);
    if(adminService.getOne(q)==null){
        return new ResponseData(null,false,"你不是管理员",300);
    }
    return new ResponseData(blogMapper.getTypeNum(),true,"获取成功",200);
}

}
